package com.jkzaixian.online;

import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.widget.Toast;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.WritableMap;
import com.jkzaixian.online.address.bean.City;
import com.jkzaixian.online.address.bean.County;
import com.jkzaixian.online.address.bean.Province;
import com.jkzaixian.online.address.bean.Street;
import com.jkzaixian.online.address.utils.LogUtil;
import com.jkzaixian.online.address.widget.AddressSelector;
import com.jkzaixian.online.address.widget.BottomDialog;
import com.jkzaixian.online.address.widget.OnAddressSelectedListener;


public class AddressActivity extends Activity {

    private BottomDialog dialog;
    private String provinceId;
    private String cityId;
    private String countyId;
    private String streetId;
    private String areaId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_address);
        toSelectArea();

        //当地址选择框消失的时候，同时要关闭容器，否则会出现空白
        dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                finish();
            }
        });
    }

    /**
     * 选择城市
     */
    private void toSelectArea() {
        if (dialog != null) {
            dialog.show();
        } else {
            dialog = new BottomDialog(AddressActivity.this);

            dialog.setOnAddressSelectedListener(new OnAddressSelectedListener() {
                @Override
                public void onAddressSelected(Province province
                        , City city
                        , County county
                        , Street street) {
                    provinceId = (province == null ? "" : province.id + "");
                    cityId = (city == null ? "" : city.id + "");
                    countyId = (county == null ? "" : county.id + "");
                    streetId = (street == null ? "" : street.id + "");
                    LogUtil.e("数据", "省份id=" + provinceId);
                    LogUtil.e("数据", "城市id=" + cityId);
                    LogUtil.e("数据", "乡镇id=" + countyId);
                    LogUtil.e("数据", "街道id=" + streetId);

                    if (!TextUtils.isEmpty(streetId)) {
                        areaId = cityId;
                    } else if (TextUtils.isEmpty(streetId) && !TextUtils.isEmpty(countyId)) {
                        areaId = countyId;
                    } else if (TextUtils.isEmpty(countyId) && !TextUtils.isEmpty(cityId)) {
                        areaId = cityId;
                    } else if (TextUtils.isEmpty(cityId) && !TextUtils.isEmpty(provinceId)) {
                        areaId = provinceId;
                    }


                    String s = (province == null ? "" : province.name) + (city == null ? "" : city.name) + (county == null ? "" : county.name) +
                            (street == null ? "" : street.name);

                    MainApplication application = (MainApplication) getApplication();

                    WritableMap params = Arguments.createMap();
                    params.putString("result", s);
                    params.putString("data", areaId);

                    application.myReactPackage.myNativeModule.sendMessage(params);
                    finish();

                    if (dialog != null) {
                        dialog.dismiss();
                    }
                }
            });

            dialog.setDialogDismisListener(new AddressSelector.OnDialogCloseListener() {
                @Override
                public void dialogclose() {
                    if (dialog != null) {
                        dialog.dismiss();
                    }
                }
            });

            dialog.setTextSize(14);//设置字体的大小
            dialog.setIndicatorBackgroundColor(android.R.color.holo_red_light);//设置指示器的颜色
            dialog.setTextSelectedColor(android.R.color.holo_red_light);//设置字体获得焦点的颜色
            dialog.setTextUnSelectedColor(android.R.color.holo_red_light);//设置字体没有获得焦点的颜色
//            dialog.setDisplaySelectorArea("31",1,"2704",1,"2711",0,"15582",1);//设置已选中的地区
            dialog.setSelectorAreaPositionListener(new AddressSelector.onSelectorAreaPositionListener() {
                @Override
                public void selectorAreaPosition(int provincePosition, int cityPosition, int countyPosition, int streetPosition) {

                }
            });
            dialog.show();
        }
    }

}
